package main

import (
	"fmt"
	"log"
	"net"

	"sync"

	pb "github.com/upmio/horus-polymerizer/benchmarktest/grpc"
	"golang.org/x/net/context"
	"google.golang.org/grpc"
)

var (
	QuestAll = 0
	Lock     sync.Mutex
)

const (
	port = ":50051"
)

type EchoServer struct{}

// SayHello implements helloworld.GreeterServer
func (s EchoServer) Echo(ctx context.Context, in *pb.EchoRequest) (*pb.EchoRespone, error) {
	Lock.Lock()
	defer Lock.Unlock()
	QuestAll++
	if QuestAll%10000 == 0 {
		fmt.Println("nun cur: is", QuestAll)
	}
	return &pb.EchoRespone{Res: int64(QuestAll)}, nil
}

func main() {
	fmt.Print("test grpc.....")
	lis, err := net.Listen("tcp", port)
	if err != nil {
		log.Fatalf("failed to listen: %v", err)
	}

	grpcServer := grpc.NewServer()
	pb.RegisterEchoServiceServer(grpcServer, EchoServer{})
	grpcServer.Serve(lis)
}
